package storage;

import parser.Content;

import java.sql.*;
import java.util.List;

public class MySQLStorage implements Storage {

    @Override
    public void storage(List<Content> contentList) throws SQLException {
        // 将 contentList 中的数据存储到 xmfish 数据库中的 table01 中

        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/xmfish", "root", "zzh211925");

        // 清空当前表格内容
        String truncateSql = "TRUNCATE TABLE table01";
        Statement truncateStmt = connection.createStatement();
        truncateStmt.executeUpdate(truncateSql);

        //将 content 中的数据存储到数据库中
        for (Content content : contentList) {
            // 设置插入语句
            String insertSql = "INSERT INTO xmfish.table01 (test,url,date) VALUES (?, ?, ?)";
            PreparedStatement pStmt = connection.prepareStatement(insertSql);
            pStmt.setString(1, content.getText());
            pStmt.setString(2, content.getUrl());
            pStmt.setString(3, content.getDate().toString());
            // 执行插入操作
            pStmt.executeUpdate();
        }
    }
}
